﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Rabbit.BusinessLogicLayer;

namespace MicroBlogClient
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class PopupdMessage : Window
    {
        DispatcherTimer timerOpenAuthenticationUrl = new DispatcherTimer();

        /// <summary>
        /// activate the main window first, other pages will add new handler on this delegate
        /// </summary>
        public Action<object, MouseButtonEventArgs> MessageClicked;
        //public Action<object, MouseButtonEventArgs> SettingsClicked;
        #region Dependency property

        public string UserPictureUrl
        {
            get { return (string)GetValue(UserPictureUrlProperty); }
            set { SetValue(UserPictureUrlProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UserPictureUrl.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UserPictureUrlProperty =
            DependencyProperty.Register("UserPictureUrl", typeof(string), typeof(PopupdMessage), new UIPropertyMetadata(string.Empty));



        public long UserID
        {
            get { return (long)GetValue(UserIDProperty); }
            set { SetValue(UserIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UserID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UserIDProperty =
            DependencyProperty.Register("UserID", typeof(long), typeof(PopupdMessage), new UIPropertyMetadata(0L));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(PopupdMessage), new UIPropertyMetadata(string.Empty));

        public int MessageCount
        {
            get { return (int)GetValue(MessageCountProperty); }
            set { SetValue(MessageCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MessageCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageCountProperty =
            DependencyProperty.Register("MessageCount", typeof(int), typeof(PopupdMessage), new UIPropertyMetadata(0));

        public string MessageType
        {
            get { return (string)GetValue(MessageTypeProperty); }
            set { SetValue(MessageTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MessageType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageTypeProperty =
            DependencyProperty.Register("MessageType", typeof(string), typeof(PopupdMessage), new UIPropertyMetadata("微博"));


        public string UserName
        {
            get { return (string)GetValue(UserNameProperty); }
            set { SetValue(UserNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UserName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UserNameProperty =
            DependencyProperty.Register("UserName", typeof(string), typeof(PopupdMessage), new UIPropertyMetadata(string.Empty));
       
        #endregion

         public PopupdMessage()
        {
            DataContext = this;
            InitializeComponent();
            this.Left = SystemParameters.WorkArea.Width - this.Width -10;
            this.Top = SystemParameters.WorkArea.Height - this.Height -10 ;
            timerOpenAuthenticationUrl.Interval = TimeSpan.FromSeconds(UserBL.CurrentUser.UserSettings.PopupMessageShowTime.Value);
            timerOpenAuthenticationUrl.Tick += new EventHandler(timerOpenAuthenticationUrl_Tick);
            timerOpenAuthenticationUrl.IsEnabled = true;
            timerOpenAuthenticationUrl.Start();

            MouseEnter += new MouseEventHandler(PopupdMessage_MouseEnter);
            MouseLeave += new MouseEventHandler(PopupdMessage_MouseLeave);
        }

         void PopupdMessage_MouseLeave(object sender, MouseEventArgs e)
         {
             timerOpenAuthenticationUrl.Start();
         }

         void PopupdMessage_MouseEnter(object sender, MouseEventArgs e)
         {
             timerOpenAuthenticationUrl.Stop();
         }

         void timerOpenAuthenticationUrl_Tick(object sender, EventArgs e)
         {
             this.Close();
         }

        #region TitleBarButton Click Event
        private void btnTitleBarClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #endregion

        private void linkShowNewMessage_Click(object sender, RoutedEventArgs e)
        {
            if (MessageClicked != null)
            {
                MessageClicked += new Action<object, MouseButtonEventArgs>((o, arg) =>
                {
                    (App.Current.MainWindow as MainWindow).ShowAndActivate();
                });
            }
            else
            {
                MessageClicked = new Action<object, MouseButtonEventArgs>((o, arg) =>
                  {
                      (App.Current.MainWindow as MainWindow).ShowAndActivate();
                  });
            }
            MessageClicked(sender, null);
            this.Close();
        } 

        private void linkOpenUser_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Process.Start(
                    string.Format("{0}{1}",
                    Application.Current.Resources["UserBlogPrefix"],
                    UserID)
                    );
            }
            catch
            {
            }
        }

        private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            (App.Current.MainWindow as MainWindow).btnTitleBarSettings_Click(sender, e);
        } 
    }
}
